package com.lianniu.mgr.cms.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.cms.model.ChannelVo;
import com.lianniu.mgr.cms.service.IChannelService;
import com.lianniu.mgr.pagination.PaginationInfo;

@Controller
@RequestMapping("/rest/Channel")
public class ChannelRestController {

private final static Logger logger = Logger.getLogger(ChannelRestController.class);
	
	@Autowired
	private IChannelService channelService;
	
	@RequestMapping(value="/ChannelList",method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody BaseResult ChannelList(HttpServletRequest request, ChannelVo curVo, PaginationInfo paginationInfo)
	 {
	       logger.info("ChannelRestController  ChannelList begin.");
	       try {
	           
	           logger.info("ChannelList begin.");          
	           BaseResult baseResult = channelService.selectByChannelForPage(curVo, paginationInfo);
	           logger.info("ChannelList end.");
	           logger.info("ChannelRestController ChannelList end.");
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("ChannelRestController ChannelList error:" + e.getMessage());
	       logger.error("ChannelRestController ChannelList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	  }
	
	@RequestMapping(value = {"/ChannelAdd"}, method = RequestMethod.POST)
	public @ResponseBody BaseResult ChannelAdd(HttpServletRequest request,ChannelVo curVo) {
	      logger.info("ChannelRestController ChannelAdd begin.");      
	      logger.info("ChannelRestController ChannelAdd:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("ChannelAdd begin.");
	        
	          BaseResult baseResult = channelService.insertChannel(curVo);
	          logger.info("ChannelAdd baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ChannelAdd end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ChannelAdd error:"+e.getMessage());
	      logger.info("ChannelRestController ChannelAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	
	 
	@RequestMapping(value = {"/ChannelEdit"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ChannelEdit(HttpServletRequest request,ChannelVo curVo) {
	      logger.info("ChannelRestController ChannelEdit begin.");      
	      logger.info("ChannelRestController ChannelEdit:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("ChannelEdit begin.");
	          BaseResult baseResult = channelService.updateById(curVo);
	          logger.info("ChannelEdit baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ChannelEdit end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ChannelRestController error:"+e.getMessage());
	      logger.info("ChannelRestController ChannelEdit end.");
	      return BaseResult.exception(e.getMessage());
	    }
	  }
	  
	
	 @RequestMapping(value="/selectChannelById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectChannelById(HttpServletRequest request,ChannelVo curVo) {
	        logger.info("ChannelRestController selectChannelById begin.");
	        try {
	                    
	          BaseResult baseResult = channelService.selectById(curVo);
	          logger.info("ChannelRestController end.");
	          logger.info("ChannelRestController selectChannelById end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ChannelRestController selectChannelById error:" + e.getMessage());
	          logger.error("ChannelRestController selectChannelById end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	 
	 @RequestMapping(value = {"/ChannelDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ChannelDel(HttpServletRequest request) {
	      logger.info("ChannelRestController ChannelDel begin.");
	     try {          
	          logger.info("ChannelDel begin.");
	          BaseResult baseResult = channelService.deleteChannelBatch(request.getParameter("ids").split(","));		
	          logger.info("ChannelDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ChannelDel end.");
	          
	          logger.info("ChannelRestController ChannelDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ChannelRestController ChannelDel error:" + e.getMessage());
	      logger.info("ChannelRestController ChannelDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	 
	 @RequestMapping(value="/selectChannel",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectParentCate(HttpServletRequest request) {
	        logger.info("ChannelRestController selectParentCate begin.");
	        try {
	                    
	          BaseResult baseResult = channelService.selectChannel();
	          logger.info("selectParentCate end.");
	          logger.info("ChannelRestController selectParentCate end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ChannelRestController selectParentCate error:" + e.getMessage());
	          logger.error("ChannelRestController selectParentCate end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	 
	 @RequestMapping(value = {"/checkChannelName"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkChannelName(HttpServletRequest request) {
	      Boolean flag=true;
	      String channelName=  request.getParameter("channelName");
	      logger.info("ChannelRestController checkChannelName begin.");
	     try {          
	          logger.info("checkChannelName begin.");
	          if("".equalsIgnoreCase(channelName)||channelName==null)
	          {return flag;}
	          flag = channelService.checkChannelName(channelName);         
	          logger.info("checkChannelName end.");
	          
	          logger.info("ChannelRestController checkChannelName end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ChannelRestController checkChannelName error:" + e.getMessage());
	      logger.info("ChannelRestController checkChannelName end.");
	      return flag;
	    }

	  }
	 
	 @RequestMapping(value = {"/checkChannelNameEdit"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkChannelNameEdit(HttpServletRequest request) {
	      Boolean flag=true;
	      String channelName=  request.getParameter("channelName");
	      String id=request.getParameter("id");
	      logger.info("ChannelRestController checkChannelNameEdit begin.");
	     try {          
	          logger.info("checkChannelNameEdit begin.");
	          ChannelVo vo=new ChannelVo();	         
	          vo.setId(id);
	          
	          BaseResult baseResult =channelService.selectById(vo);	          
	          ChannelVo curVo=(ChannelVo) baseResult.getData();
	          if(curVo.getId().equals(id)&&curVo.getChannelName().equals(channelName))
	          {return flag;}
	          if("".equalsIgnoreCase(channelName)||channelName==null)
	          {return flag;}
	          flag = channelService.checkChannelName(channelName);         
	          logger.info("checkChannelNameEdit end.");
	          
	          logger.info("ChannelRestController checkChannelNameEdit end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ChannelRestController checkChannelNameEdit error:" + e.getMessage());
	      logger.info("ChannelRestController checkChannelNameEdit end.");
	      return flag;
	    }

	  }
}
